package com.obao.business.dao.impl;

import com.obao.business.dao.IBusinessDao;
import com.obao.business.dao.IProductDao;
import com.obao.business.entity.Business;
import com.obao.business.entity.BusinessDomain;
import com.obao.business.entity.Product;
import com.obao.business.entity.ProductSort;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;

import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */
public class BusinessDao implements IBusinessDao{
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


	@Override
	public List<BusinessDomain> findDomains() {
		return  sessionFactory.getCurrentSession().createQuery("from BusinessDomain").list();
	}

    @Override
    public Business findBusinessById(Integer id) {

        return (Business) sessionFactory.getCurrentSession().get(Business.class,id);
    }

    @Override
    public List<Business> findBusinessList(Integer id) {
        return sessionFactory.getCurrentSession().createQuery("from Business where domainId=?")
                .setParameter(0,id).list();
    }

    @Override
    public Integer findBusinessIdByProduct(Product product) {
        String sql = "SELECT  business_id from t_product where product_id="+product.getProductId();
        Integer businessId = (Integer) sessionFactory.getCurrentSession().createSQLQuery(sql.toString()).uniqueResult();
       return businessId;
    }
}
